package com.bgs.eat.login.service.impl;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.login.mapper.LoginMapper;
import com.bgs.eat.login.service.ShopLoginService;
import com.bgs.eat.userProtal.entity.UserProtalEntity;
import com.bgs.eat.userProtal.mapper.UserProtalMapper;
import com.bgs.eat.utils.RedisUtil;
import com.bgs.eat.utils.ShortMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.util.DigestUtils;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;

/**
 * @ClassName ShopLoginServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2022/10/14 10:45
 **/
@Service
public class ShopLoginServiceImpl implements ShopLoginService {

    @Autowired
    LoginMapper loginMapper;

    @Autowired
    UserProtalMapper userProtalMapper;

    @Autowired
    RedisUtil redisUtil;

    /**
    * @Description 店铺根据账号登录
    * @Author lzy
    * @Date 2022/10/14 10:45
    * @Return com.bgs.eat.config.CommonResult
    * @param userName
    * @param password
    */
    @Override
    public CommonResult loginByUserRequest(String userName, String password) {
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        HashMap<Object, Object> map = new HashMap<>();
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
        userProtalEntity.setPassword(md5Str);
        userProtalEntity.setUserName(userName);
        UserProtalEntity userProtalEntity1 = userProtalMapper.selectOne(userProtalEntity);
        String token = Base64Utils.encodeToString((userProtalEntity.getUserName()+new Date()+userProtalEntity.getPassword()).getBytes());
        if(userProtalEntity1 != null) {
            map.put("user", userProtalEntity1);
            map.put("token", token);
            return CommonResult.ok(map);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 商家app 根据手机号登录/注册（门户展示）
    * @Author lzy
    * @Date 2022/10/17 8:39
    * @Return com.bgs.eat.config.CommonResult
    * @param phone
    * @param code
    * @param manner
    */
    @Override
    public CommonResult loginByPhoneRequest(String phone, String code, String manner) {
        Object o = redisUtil.get(phone + manner);
        if (code.equals(o)) {
            HashMap<Object, Object> map = new HashMap<>();
            UserProtalEntity userProtalEntity = new UserProtalEntity();
            userProtalEntity.setPhone(phone);
            UserProtalEntity userProtalEntity1 = userProtalMapper.selectOne(userProtalEntity);
            if (userProtalEntity1 != null) {
                String token = Base64Utils.encodeToString((phone+new Date()).getBytes());
                map.put("token", token);
                map.put("user", userProtalEntity1);
                return CommonResult.ok(map);
            } else {
                String token = Base64Utils.encodeToString((phone+new Date()).getBytes());
                userProtalEntity.setCreateTime(new Date());
                userProtalEntity.setDelFlag(0);
                userProtalEntity.setAvatarId(1);
                userProtalEntity.setRole(1);
                userProtalEntity.setAuditId(0);
                int insert = userProtalMapper.insert(userProtalEntity);
                map.put("token", token);
                map.put("user", userProtalEntity);
                if (insert > 0) {
                    return CommonResult.ok(map);
                } else {
                    return CommonResult.error("500");
                }
            }
        } else {
            return CommonResult.error("1");
        }
    }

    /**
    * @Description 商家app发送验证码（门户展示）
    * @Author lzy
    * @Date 2022/10/17 8:50
    * @Return com.bgs.eat.config.CommonResult
    * @param phone
    */
    @Override
    public CommonResult shopLoginGoCode(String phone) {
        String manner = "loginAndRegister";
        ShortMessageUtils.sendPhoeRedis(phone, manner);
        return CommonResult.ok(manner);
    }

    /**
     * @Author G胖
     * @Description 商家app提现时绑定支付宝账号
     * @Date 2022-10-28 上午 10:50
     * @Return

     */
    @Override
    public Integer alipayLoginName(UserProtalEntity userProtalEntity) {
        return userProtalMapper.updateByPrimaryKeySelective(userProtalEntity);
    }

    /**
     * @Author G胖
     * @Description 查询商家是否绑定了支付宝账号
     * @Date 2022-10-28 下午 01:37
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public UserProtalEntity getShopLoginName(Integer id) {
        UserProtalEntity userProtalEntity = new UserProtalEntity();
        userProtalEntity.setId(id);
        return userProtalMapper.selectOne(userProtalEntity);
    }
}
